<template>
  <div class="course-card" @click="$emit('click')">
    <div class="course-image">
      <img :src="course.cover || '/default-course.jpg'" :alt="course.title" />
      <div class="course-badge" v-if="course.badge">
        {{ course.badge }}
      </div>
    </div>
    
    <div class="course-content">
      <div class="course-header">
        <h3 class="course-title">{{ course.title }}</h3>
        <div class="course-price">
          <span v-if="course.price > 0" class="price">¥{{ course.price }}</span>
          <span v-else class="free">免费</span>
        </div>
      </div>
      
      <p class="course-description">{{ course.description }}</p>
      
      <div class="course-meta">
        <div class="meta-item">
          <el-icon><User /></el-icon>
          <span>{{ course.studentCount || 0 }}人学习</span>
        </div>
        <div class="meta-item">
          <el-icon><Star /></el-icon>
          <span>{{ course.rating || 0 }}分</span>
        </div>
      </div>
      
      <div class="course-tags">
        <el-tag
          v-for="tag in course.tags"
          :key="tag"
          size="small"
          :type="getTagType(tag)"
        >
          {{ tag }}
        </el-tag>
      </div>
      
      <div class="course-progress" v-if="course.progress">
        <el-progress 
          :percentage="course.progress" 
          :stroke-width="4"
          :show-text="false"
          :color="getProgressColor(course.progress)"
        />
        <span class="progress-text">已学习 {{ course.progress }}%</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { User, Star } from '@element-plus/icons-vue'

const props = defineProps({
  course: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['click'])

const getTagType = (tag) => {
  const typeMap = {
    '初级': 'primary',
    '中级': 'warning',
    '高级': 'danger',
    '热门': 'danger',
    '新课': 'success'
  }
  return typeMap[tag] || 'info'
}

const getProgressColor = (percentage) => {
  if (percentage < 30) {
    return '#f56c6c'
  } else if (percentage < 70) {
    return '#e6a23c'
  } else {
    return '#67c23a'
  }
}
</script>

<style scoped>
.course-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(44, 90, 160, 0.1);
  transition: all 0.3s;
  cursor: pointer;
  border: 1px solid #e6e8ea;
}

.course-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(44, 90, 160, 0.15);
  border-color: #2c5aa0;
}

.course-image {
  position: relative;
  height: 160px;
  overflow: hidden;
}

.course-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.course-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: #2c5aa0;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.course-content {
  padding: 16px;
}

.course-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.course-title {
  font-size: 16px;
  font-weight: bold;
  color: #2c5aa0;
  margin: 0;
  flex: 1;
  line-height: 1.4;
}

.course-price {
  margin-left: 12px;
  white-space: nowrap;
}

.price {
  color: #2c5aa0;
  font-weight: bold;
  font-size: 18px;
}

.free {
  color: #67c23a;
  font-weight: bold;
}

.course-description {
  color: #606266;
  font-size: 14px;
  line-height: 1.4;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-meta {
  display: flex;
  gap: 16px;
  margin-bottom: 12px;
  font-size: 13px;
  color: #909399;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.course-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.course-progress {
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-text {
  font-size: 12px;
  color: #606266;
  white-space: nowrap;
}
</style>